<?php
namespace App\Models\Traits;
use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;

trait recordLastActivedAtHelper{
    protected $hash_prefix = 'luntan1_last_actived_at';
    protected $field_prefix = 'user_';

    public function recordLastActivedAt(){
        $date = Carbon::now()->toDateString();
        $hash = $this->hash_prefix.$date;
        $field = $this->field_prefix.$this->id;
        //dd(Redis::hGetAll($hash));
        $now = Carbon::now()->toDateTimeString();
        Redis::hSet($hash,$field,$now);
    }
    public function syncUserActivedAt(){
        $yesterday_date = Carbon::yesterday()->toDateString();
        //$yesterday_date = Carbon::now()->toDateString();
        $hash = $this->hash_prefix.$yesterday_date;
        $dates = Redis::hGetAll($hash);
        foreach ($dates as $user_id=>$active_at){
            $user_id = str_replace($this->field_prefix,'',$user_id);
            if($user = $this->find($user_id)){
                $user->last_actived_at = $active_at;
                $user->save();
            }
        }
        Redis::del($hash);
    }
    public function getLastActivedAtAttribute($value){
        $date = Carbon::now()->toDateString();
        $hash = $this->hash_prefix.$date;
        $field = $this->field_prefix.$this->id;
        $datetime = Redis::hGet($hash,$field) ?: $value;
        if($datetime){
            return new Carbon($datetime);
        }else{
            return $this->created_at;
        }

    }

}
